package com.example.cpms.controller;
import com.example.cpms.entity.HouseHold;
import com.example.cpms.service.HouseHoldService;
import com.example.cpms.utils.PageBean;
import com.example.cpms.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Slf4j
@RestController
public class HouseHoldController {

    @Autowired
    private HouseHoldService householdService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/household")  //改用PageHelper插件
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,//前端传入要查询的页数以及每页显示多少条数据
                       String name,String idCart,String phone){ //模糊查询额外传入的值，可选
        log.info("住房管理分页查询：{}，{},{},{},{}",page,pageSize,name,idCart,phone); //输出日志信息
        PageBean pageBean = householdService.page(page,pageSize,name,idCart,phone); //把前端传入的信息传到service层
        return Result.success(pageBean);//返回数据
    }


    /**
     *根据id删除住户
     * @return
     */
    @DeleteMapping("/household/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除住户：{}",id);
        householdService.delete(id);
        return Result.success();
    }

    /**
     * 新增住户
     * @param household
     * @return
     */
    @PostMapping("/household")
    public Result insert(@RequestBody HouseHold household){
        log.info("新增住户：{}",household);//打印日志信息
        householdService.add(household);
        return Result.success();
    }

    /**
     * 编辑住户信息
     * @param household
     * @return
     */
    @PutMapping("/household")
    public Result updata(@RequestBody HouseHold household){
        log.info("编辑住户：{}",household);
        householdService.update(household);
        return Result.success();
    }



}
